#include <stdio.h>

#include <CompileVisitor.h>

#include "AstNodeIfStatement.h"
#include "AstNodeBooleanOperator.h"



AstNodeIfStatement::AstNodeIfStatement(AstNodeBooleanOperator * cond,
    AstNode * true_body,
    AstNode * false_body)
{
  this->condition=cond;
  this->true_body=true_body;
  this->false_body=false_body;
}

#if 0
int AstNodeIfStatement::compile(ClassFile * klass,MethodInfo * method)
{
  u4 targetFalse;
  u4 targetTrue;

  RelocTable relocOnTrue;
  RelocTable relocOnFalse;

  condition->compileWithReloc(klass,method,relocOnTrue,relocOnFalse);

  targetTrue=method->getAP();

  true_body->compile(klass,method);

  targetFalse=method->getAP();
  if(false_body) {
    u4 relocOnEnd=method->getAP();
    method->assemble(OPC_goto,0);
    targetFalse=method->getAP();
    false_body->compile(klass,method);
    method->relocAt(relocOnEnd,method->getAP());
  }

  relocOnFalse.reloc(method,targetFalse);
  relocOnTrue.reloc(method,targetTrue);
  return 0;
}
#endif

void AstNodeIfStatement::dump(int level)
{
  printf("IF\n");
  condition->dump(level+1);

  printf("THEN\n");
  true_body->dump(level+1);

  printf("ELSE\n");
  false_body->dump(level+1);
}

void AstNodeIfStatement::compile(CompileVisitor * visitor)
{
  visitor->compile(this);
}
